<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\admin\lib\Layui;
use app\admin\logic\Banner as LogicBanner;
use app\admin\validate\Banner as ValidateBanner;

class Banner extends Base
{
    /**
     * 数据表格
     */
    public function data(int $page = 1, int $limit = 10)
    {
        $data = LogicBanner::getDataTable($page, $limit);
        return Layui::table('banner图', ...array_values($data));
    }

    /**
     * 添加
     *
     * @param array $data
     */
    public function save(array $data)
    {
        try {
            validate(ValidateBanner::class)->check($data);
            LogicBanner::save($data);
        } catch (\Exception $e) {
            return wrong($e->getMessage());
        }
        return success('添加成功');
    }

    /**
     * 编辑
     *
     * @param int $id
     */
    public function read(int $id)
    {
        try {
            $data = LogicBanner::read($id);
        } catch (\Exception $e) {
            return wrong($e->getMessage());
        }
        return view('', compact('data'));
    }

    /**
     * 更新
     *
     * @param int    $id
     * @param array  $data
     */
    public function update(int $id, array $data)
    {
        try {
            validate(ValidateBanner::class)->check($data);
            LogicBanner::update($id, $data);
        } catch (\Exception $e) {
            return wrong($e->getMessage());
        }
        return success('修改成功');
    }

    /**
     * 删除
     *
     * @param int|array $id
     */
    public function delete($id)
    {
        try {
            LogicBanner::delete($id);
        } catch (\Exception $e) {
            return wrong($e->getMessage());
        }
        return success('删除成功');
    }
}
